package com.ruoyi.supplier.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.supplier.domain.bo.RefundItemBo;
import com.ruoyi.supplier.domain.vo.RefundItemVo;
import com.ruoyi.supplier.domain.RefundItem;
import com.ruoyi.supplier.mapper.RefundItemMapper;
import com.ruoyi.supplier.service.IRefundItemService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 退货商品明细Service业务层处理
 *
 * @author ruoyi
 * @date 2023-09-18
 */
@RequiredArgsConstructor
@Service
@DS("slave")
public class RefundItemServiceImpl implements IRefundItemService {

    private final RefundItemMapper baseMapper;

    /**
     * 查询退货商品明细
     */
    @Override
    public RefundItemVo queryById(Integer refundItemId){
        return baseMapper.selectVoById(refundItemId);
    }

    /**
     * 查询退货商品明细列表
     */
    @Override
    public TableDataInfo<RefundItemVo> queryPageList(RefundItemBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<RefundItem> lqw = buildQueryWrapper(bo);
        Page<RefundItemVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询退货商品明细列表
     */
    @Override
    public List<RefundItemVo> queryList(RefundItemBo bo) {
        LambdaQueryWrapper<RefundItem> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<RefundItem> buildQueryWrapper(RefundItemBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<RefundItem> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getSupplierId() != null, RefundItem::getSupplierId, bo.getSupplierId());
        lqw.eq(bo.getMerchantId() != null, RefundItem::getMerchantId, bo.getMerchantId());
        lqw.eq(bo.getSkuId() != null, RefundItem::getSkuId, bo.getSkuId());
        lqw.eq(bo.getShouldNum() != null, RefundItem::getShouldNum, bo.getShouldNum());
        lqw.eq(bo.getOrderId() != null, RefundItem::getOrderId, bo.getOrderId());
        lqw.eq(bo.getShopId() != null, RefundItem::getShopId, bo.getShopId());
        lqw.eq(bo.getShopSkuId() != null, RefundItem::getShopSkuId, bo.getShopSkuId());
        return lqw;
    }

    /**
     * 新增退货商品明细
     */
    @Override
    public Boolean insertByBo(RefundItemBo bo) {
        RefundItem add = BeanUtil.toBean(bo, RefundItem.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setRefundItemId(add.getRefundItemId());
        }
        return flag;
    }

    /**
     * 修改退货商品明细
     */
    @Override
    public Boolean updateByBo(RefundItemBo bo) {
        RefundItem update = BeanUtil.toBean(bo, RefundItem.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(RefundItem entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除退货商品明细
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Integer> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
